<?php
namespace App\Models\Authority\Method;

use App\Models\Authority\Interfaces\AuthorityInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class cxhUser implements AuthorityInterface
{
    /**
     * 车享惠账号登录
     * @param \App\Libraries\CodeEnum $code
     * @param $uID
     * @return array|void
     */
    public function Login(&$code, $uID)
    {
        $field = ['id', 'cxhname', 'level', 'openid', 'realname', 'mobile', 'idcard', 'nickname', 'credit1', 'credit2', 'credit3', 'credit4', 'credit5', 'avatar', 'isblack', 'invite_qrcode'];
        $memberInfo = DB::connection("jmft_cxh")->table("ims_ewei_shop_member")
            ->where('member_uuid', $uID)->first($field);
        if ($memberInfo) {
            $memInfo = (array) $memberInfo;
            //查询扩展表信息
            $exField = ['uex_level', 'uex_agent', 'uex_areaName', 'uex_cityNname', 'uex_provinceName', 'uex_parentID', 'uex_parentAccount', 'uex_parentList'];
            $userEx = DB::connection("jmft_cxh")->table("ims_cxh_user_ex")
                ->where('member_id', $memInfo['id'])->first($exField);
            if ($userEx) {
                $exData = (array) $userEx;
                $memInfo['user_ex'] = $exData;
            } else {
                $exData = [];
                $memInfo['user_ex'] = $exData;
            }
            unset($memInfo['id']);
        } else {
            $memInfo = [];
        }
        return $memInfo;
    }

    /**
     * 车享惠注册接口
     * @param \App\Libraries\CodeEnum $code
     * @param $uInfo
     * @param \App\Models\Authority\Interfaces\Request $rqt
     * @return array|void
     */
    public function Reg(&$code, $uInfo,  $rqt)
    {
        $cxhname = isset($uInfo['username']) ? $uInfo['username'] : '';
        $memberUuid = isset($uInfo['usersid']) ? $uInfo['usersid'] : '';
        $preName  = substr($cxhname,0,-6);
        if($preName == "dls" || $preName == "DLS"){//代理商时候。只是添加member_uuid
            //修改uuid
            //查询该用户是否存在
            $agent = DB::connection("jmft_cxh") -> table("ims_ewei_shop_member") -> where('cxhname',$cxhname) -> get();
            if($agent){
                DB::connection("jmft_cxh") -> table("ims_ewei_shop_member") -> where('cxhname',$cxhname) -> update(['member_uuid'=>$memberUuid]);
            }
            $data = [];
            return $data;
        }else{
            $mobile = isset($uInfo['phone']) ? $uInfo['phone'] : '';
            $password = isset($uInfo['password']) ? $uInfo['password'] : '';
            $salt = isset($uInfo['user_code']) ? $uInfo['user_code'] : '';
            $avartar = isset($uInfo['head_pic']) ? $uInfo['head_pic'] : '';
            $realname = isset($uInfo['real_name']) ? $uInfo['real_name'] : '';
            $idcard = isset($uInfo['cardID']) ? $uInfo['cardID'] : '';
            $parent = isset($rqt['parentUID']) ? $rqt['parentUID'] : '';
            $payPwd = isset($rqt['pay_pwd']) ? $rqt['pay_pwd'] : '';
            $addData['cxhname'] = $cxhname;
            $addData['member_uuid'] = $memberUuid;
            $addData['mobile'] = $mobile;
            $addData['pwd'] = $password;
            $addData['salt'] = $salt;
            $addData['avatar'] = $avartar;
            $addData['realname'] = $realname;
            $addData['idcard'] = $idcard;
            $addData['openid'] = 'wap_user_' . '1' . '_' . $cxhname;
            $addData['pay_passwd'] = md5($payPwd);
            $addData['createtime'] = time();
            $addData['mobileverify'] = 1;
            $addData['comefrom'] = 'mobile';
            $addData['uniacid'] = 1;
            $addData['level'] = 1;
            //查询到上级信息
            $pInfo = DB::connection("jmft_cxh") -> table("ims_ewei_shop_member")
                -> where('member_uuid',$parent)
                -> first(['id','cxhname']);
            $parent = [];
            if($pInfo){
                $parent = (array)$pInfo;
            }else{
                $parent['id'] = 1;//找不到推荐人，就默认为总部；
                $parent['cxhname'] = 'DLS111111';//找不到推荐人，就默认为总部；
            }
            //开启事务：
            DB::connection("jmft_cxh") -> beginTransaction();
            $memId = DB::connection("jmft_cxh") -> table("ims_ewei_shop_member") -> insertGetId($addData);
            //查询到上级的扩展信息
            $pExInfo = DB::connection("jmft_cxh") -> table("ims_cxh_user_ex")
                -> where('member_id',$parent)
                -> first(['id','uex_parentAccount','uex_parentList']);
            if($pExInfo){
                $pExInfo = (array)$pExInfo;
            }else{
                $pExInfo['uex_parentAccount'] = '';
                $pExInfo['uex_parentList'] = '';
            }
            //添加扩展表信息
            $exData = [
                'member_id'=>$memId,
                'member_uuid'=>$memberUuid,
                'uex_level'=>1,//普通
                'uex_agent'=>0,//会员
                'uex_parentID'=>$parent['id'],//会员
                'uex_parentAccount'=>$parent['cxhname'],//上级账号
                'uex_parentList'=>$pExInfo['uex_parentList'].$memId.',',//关系树
                'ctime'=>time(),
                'utime'=>time(),
            ];
            $userExId =  DB::connection("jmft_cxh") -> table("ims_cxh_user_ex") -> insertGetId($exData);
            if($memId && $userExId){
                //查询出来当前注册的信息
                $memfield = ['id', 'cxhname', 'level', 'openid', 'realname', 'mobile', 'idcard', 'nickname', 'credit1', 'credit2', 'credit3', 'credit4', 'credit5', 'avatar', 'isblack', 'invite_qrcode'];
                $memData = DB::connection("jmft_cxh")->table("ims_ewei_shop_member")
                    ->where('member_uuid', $memberUuid)->first($memfield);
                if($memData){
                    $data = (array)$memData;
                }else{
                    $data = [];
                }
                $exField = ['uex_level', 'uex_agent', 'uex_areaName', 'uex_cityNname', 'uex_provinceName', 'uex_parentID', 'uex_parentAccount', 'uex_parentList'];
                $userExData =  DB::connection("jmft_cxh")->table("ims_cxh_user_ex")
                    ->where('member_id', $memId['id'])->first($exField);
                if($userExData){
                    $data['user_ex'] = (array)$userExData;
                }else{
                    $data['user_ex'] = [];
                }
                DB::connection("jmft_cxh") -> commit();
            }else{
                DB::connection("jmft_cxh") ->rollBack();
                $data = [];
            }
            return $data;
        }
    }
    public function Reg12(&$code, $uInfo,  $rqt)
    {
        $cxhname = isset($uInfo['username']) ? $uInfo['username'] : '';
        $memberUuid = isset($uInfo['usersid']) ? $uInfo['usersid'] : '';
        $mobile = isset($uInfo['phone']) ? $uInfo['phone'] : '';
        $password = isset($uInfo['password']) ? $uInfo['password'] : '';
        $salt = isset($uInfo['user_code']) ? $uInfo['user_code'] : '';
        $avartar = isset($uInfo['head_pic']) ? $uInfo['head_pic'] : '';
        $realname = isset($uInfo['real_name']) ? $uInfo['real_name'] : '';
        $idcard = isset($uInfo['cardID']) ? $uInfo['cardID'] : '';
        $parent = isset($rqt['parent']) ? $rqt['parent'] : '';
        $payPwd = isset($rqt['pay_pwd']) ? $rqt['pay_pwd'] : '';
        $addData['cxhname'] = $cxhname;
        $addData['member_uuid'] = $memberUuid;
        $addData['mobile'] = $mobile;
        $addData['pwd'] = $password;
        $addData['salt'] = $salt;
        $addData['avatar'] = $avartar;
        $addData['realname'] = $realname;
        $addData['idcard'] = $idcard;
        $addData['openid'] = 'wap_user_' . '1' . '_' . $cxhname;
        $addData['pay_passwd'] = md5($payPwd);
        $addData['createtime'] = time();
        $addData['mobileverify'] = 1;
        $addData['comefrom'] = 'mobile';
        $addData['uniacid'] = 1;
        $addData['level'] = 1;
        //查询到上级信息
        $pInfo = DB::connection("jmft_cxh") -> table("ims_ewei_shop_member")
            -> where('id',$parent)
            ->orWhere('mobile',$parent)
            ->orWhere('cxhname',$parent)
            -> first(['id','cxhname']);
        $parent = [];
       if($pInfo){
           $parent = (array)$pInfo;
       }else{
           $parent['id'] = 1;//找不到推荐人，就默认为总部；
           $parent['cxhname'] = 'DLS111111';//找不到推荐人，就默认为总部；
       }
        //开启事务：
        DB::connection("jmft_cxh") -> beginTransaction();
        $memId = DB::connection("jmft_cxh") -> table("ims_ewei_shop_member") -> insertGetId($addData);
        //查询到上级的扩展信息
        $pExInfo = DB::connection("jmft_cxh") -> table("ims_cxh_user_ex")
            -> where('member_id',$parent)
            -> first(['id','uex_parentAccount','uex_parentList']);
        if($pExInfo){
            $pExInfo = (array)$pExInfo;
        }else{
            $pExInfo['uex_parentAccount'] = '';
            $pExInfo['uex_parentList'] = '';
        }
        //添加扩展表信息
        $exData = [
            'member_id'=>$memId,
            'member_uuid'=>$memberUuid,
            'uex_level'=>1,//普通
            'uex_agent'=>0,//会员
            'uex_parentID'=>$parent['id'],//会员
            'uex_parentAccount'=>$parent['cxhname'],//上级账号
            'uex_parentList'=>$pExInfo['uex_parentList'].$memId.',',//关系树
            'ctime'=>time(),
            'utime'=>time(),
        ];
        $userExId =  DB::connection("jmft_cxh") -> table("ims_cxh_user_ex") -> insertGetId($exData);
        if($memId && $userExId){
           //查询出来当前注册的信息
            $memfield = ['id', 'cxhname', 'level', 'openid', 'realname', 'mobile', 'idcard', 'nickname', 'credit1', 'credit2', 'credit3', 'credit4', 'credit5', 'avatar', 'isblack', 'invite_qrcode'];
            $memData = DB::connection("jmft_cxh")->table("ims_ewei_shop_member")
                ->where('member_uuid', $memberUuid)->first($memfield);
            if($memData){
                $data = (array)$memData;
            }else{
                $data = [];
            }
            $exField = ['uex_level', 'uex_agent', 'uex_areaName', 'uex_cityNname', 'uex_provinceName', 'uex_parentID', 'uex_parentAccount', 'uex_parentList'];
            $userExData =  DB::connection("jmft_cxh")->table("ims_cxh_user_ex")
                ->where('member_id', $memId['id'])->first($exField);
            if($userExData){
                $data['user_ex'] = (array)$userExData;
            }else{
                $data['user_ex'] = [];
            }
            DB::connection("jmft_cxh") -> commit();
        }else{
            DB::connection("jmft_cxh") ->rollBack();
            $data = [];
        }
        return $data;
    }
    public function Logout(&$code, $uID)
    {

    }

    /**
     * 修改头像和真实姓名
     * @param \App\Libraries\CodeEnum $code
     * @param $uInfo
     * @param \App\Models\Authority\Interfaces\Request $rqt
     */
    public function Edit(&$code, $uInfo, $rqt)
    {
        $uid = $uInfo['usersid'];
        $avatar = isset($uInfo['head_pic']) ? $uInfo['head_pic'] : '';
        $realName = isset($uInfo['real_name']) ? $uInfo['real_name'] : '';
        $phone = isset($uInfo['phone']) ? $uInfo['phone'] : '';
        $saveData = [];
        if(!empty($avatar)){
            $saveData['avatar'] = $avatar;
        }
        if(!empty($realName)){
            $saveData['realname'] = $realName;
        }
        if(!empty($phone)){
            $saveData['mobile'] = $phone;
        }
        DB::connection("jmft_cxh") -> table("ims_ewei_shop_member") ->where('member_uuid',$uid) -> update($saveData);
    }
}
